package com.org.sort;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ForkJoinTask;

@SuppressWarnings("serial")
public class ListConcurrentSortComparator<T> extends AbConcurrentListSort<T> {

    private Comparator<? super T> comparator;

    private ListConcurrentSortComparator(List<T> list, int start, int end, int threadloads,
            Comparator<? super T> comparator) {
        super(list, start, end, threadloads);
        this.comparator = comparator;
    }

    public ListConcurrentSortComparator(List<T> list, int threadloads, Comparator<? super T> comparator) {
        super(list, threadloads);
        this.comparator = comparator;
    }

    public ListConcurrentSortComparator(List<T> list, Comparator<? super T> comparator) {
        super(list);
        this.comparator = comparator;
    }

    @Override
    public void sortRange(int start, int end) {
        Collections.sort(list.subList(start, end), comparator);
    }

    @Override
    protected int compare(T t1, T t2) {
        return comparator.compare(t1, t2);
    }

    @Override
    protected ForkJoinTask<Void> invokeTaskLeft(int pos) {

        return new ListConcurrentSortComparator<>(list, began, pos - 1, threadloads, comparator);
    }

    @Override
    protected ForkJoinTask<Void> invokeTaskRight(int pos) {

        return new ListConcurrentSortComparator<>(list, pos + 1, end, threadloads, comparator);
    }

}
